﻿using System;
using Cysharp.Threading.Tasks;
using LFloatMath.Math;

namespace IQIGame.Onigao.GamePlay
{
    public class BattleImpactComponent : TComponent
    {
        private UnitView unit;

        public async UniTaskVoid Impact(LVector2 targetPos, LFloat speed, LVector2 dir)
        {
            var rTimelineComp = this.unit.GetComponent<BattleTimelineComponent>(ETComponentType.BattleTimelineComponent);
            rTimelineComp.enableLoopClip = true;

            var rMoveComp = this.unit.GetComponent<BattleMoveComponent>(ETComponentType.BattleMoveComponent);
            await rMoveComp.Impact(targetPos, speed, dir);

            rTimelineComp.enableLoopClip = false;
            this.unit.pos = targetPos;
            
            var rViewComp = this.unit.GetComponent<UnitViewComponent>(ETComponentType.UnitViewComponent);
            await rViewComp.SyncPos();
        }


        protected override void OnDispose()
        {
        }

        public override void OnCreate(TEntity entity)
        {
            this.unit = entity as UnitView;
        }
    }
}